<template>
  注册页面

  <van-form @failed="onFailed" @submit="onSubmit">
    <van-cell-group inset>
      <!-- 通过 pattern 进行正则校验 -->
      <van-field
        label="用户名"
        v-model="username"
        name="username"
        placeholder="请输入用户名"
        :rules="[{ pattern: /\w{3,}/, message: '三位以上 的数字字母下划线' }]"
        required
        :clearable="true"
      />

      <van-field
        type="password"
        label="密码"
        v-model="password"
        name="password"
        placeholder="请输入密码"
        :rules="[{ pattern: /.{6,}/, message: '三位以上 的任意字符' }]"
        required
        clearable
      />
    </van-cell-group>
    <div style="margin: 16px">
      <van-button round block type="primary" native-type="submit"> 提交 </van-button>
    </div>
  </van-form>
</template>

<script setup>
import { ref } from "vue";
import { regRequest } from "@/api/user";
import router from "@/router";
import { showSuccessToast, showFailToast } from "vant";

// /\w{3,}/
// 三位以上 的数字字母下划线
let username = ref("");
let password = ref("");

function onFailed() {
  console.log("onFailed");
}
// 表单校验成功就会执行
async function onSubmit() {
  console.log("onSubmit");
  console.log(username.value);
  console.log(password.value);
  let r = await regRequest(username.value, password.value);
  console.log(r.data);
  if (r.data.code == 200) {
    router.push("/login");
    showSuccessToast(r.data.message);
  } else {
    showFailToast(r.data.message);
  }
}
</script>

<style></style>
